<template>
  <AdminLayout>
    <div class="admin-container">
      <div class="page-header">
        <h1>企业管理</h1>
        <p class="page-description">管理关于我们页面的企业信息</p>
      </div>

      <!-- 标签切换 -->
      <div class="tab-container">
        <button 
          class="tab-button" 
          :class="{ active: activeTab === 'people' }"
          @click="switchTab('people')"
        >
          人物简介
        </button>
        <button 
          class="tab-button" 
          :class="{ active: activeTab === 'milestones' }"
          @click="switchTab('milestones')"
        >
          企业发展历程
        </button>
      </div>

      <!-- 人物简介管理 -->
      <div v-if="activeTab === 'people'" class="tab-content">
        <div class="search-actions">
          <button class="primary-button" @click="openAddPersonModal">
            添加人物简介
          </button>
        </div>

        <div class="content-wrapper">
          <!-- 人物列表 -->
          <div class="people-grid">
            <div v-for="person in companyPeople" :key="person.id" class="person-item">
              <div class="person-image-container">
                <img :src="person.imageUrl" :alt="person.name" class="person-image">
              </div>
              <div class="person-info">
                <h3>{{ person.name || '未设置姓名' }}</h3>
                <p class="person-position">{{ person.position || '未设置职位' }}</p>
                <div class="person-meta">
                  <span class="date-label">{{ formatDate(person.updatedAt) }}</span>
                </div>
                <div class="person-actions">
                  <button class="edit-button" @click="editPerson(person)">
                    编辑
                  </button>
                  <button class="delete-button" @click="confirmDelete('people', person.id)">
                    删除
                  </button>
                </div>
              </div>
            </div>
          </div>

          <!-- 空状态 -->
          <div v-if="companyPeople.length === 0" class="empty-state">
            <p>暂无人物简介数据，请点击上方按钮添加</p>
          </div>
        </div>
      </div>

      <!-- 企业发展历程管理 -->
      <div v-if="activeTab === 'milestones'" class="tab-content">
        <div class="search-actions">
          <button class="primary-button" @click="openAddMilestoneModal">
            添加发展历程
          </button>
        </div>

        <div class="content-wrapper">
          <!-- 历程列表 -->
          <div class="milestones-list">
            <div v-for="milestone in companyMilestones" :key="milestone.id" class="milestone-item">
              <div class="milestone-year">{{ milestone.year || '未设置年份' }}</div>
              <div class="milestone-content">
                <h3>{{ milestone.title || '未设置标题' }}</h3>
                <div v-if="milestone.imageUrl" class="milestone-image">
                  <img :src="milestone.imageUrl" :alt="milestone.title" class="small-image">
                </div>
                <p class="milestone-description">{{ milestone.description || '无描述' }}</p>
                <div class="milestone-meta">
                  <span class="date-label">{{ formatDate(milestone.updatedAt) }}</span>
                </div>
                <div class="milestone-actions">
                  <button class="edit-button" @click="editMilestone(milestone)">
                    编辑
                  </button>
                  <button class="delete-button" @click="confirmDelete('milestones', milestone.id)">
                    删除
                  </button>
                </div>
              </div>
            </div>
          </div>

          <!-- 空状态 -->
          <div v-if="companyMilestones.length === 0" class="empty-state">
            <p>暂无发展历程数据，请点击上方按钮添加</p>
          </div>
        </div>
      </div>
    </div>
  </AdminLayout>

  <!-- 人物简介弹窗 -->
  <div v-if="showPersonModal" class="modal-overlay" @click="closePersonModal">
    <div class="modal-content" @click.stop>
      <div class="modal-header">
        <h3>{{ isEditingPerson ? '编辑人物简介' : '添加人物简介' }}</h3>
        <button class="modal-close" @click="closePersonModal">&times;</button>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <label data-required="*">姓名</label>
          <input v-model="currentPerson.name" type="text" placeholder="请输入姓名">
        </div>
        <div class="form-group">
          <label data-required="*">职位</label>
          <input v-model="currentPerson.position" type="text" placeholder="请输入职位">
        </div>
        <div class="form-group">
          <label>简介</label>
          <textarea v-model="currentPerson.bio" placeholder="请输入简介" rows="4"></textarea>
        </div>
        <div class="form-group">
          <label data-required="*">头像</label>
          <div class="image-upload-container">
            <input ref="personFileInput" type="file" accept="image/*" style="display: none" @change="handlePersonImageUpload">
            <button class="secondary-button" @click="triggerPersonFileInput">选择图片</button>
            <button v-if="currentPerson.imageUrl" class="delete-button" @click="removePersonImage">移除图片</button>
          </div>
          <div v-if="currentPerson.imageUrl" class="preview-image-container">
            <img :src="currentPerson.imageUrl" alt="预览图" class="preview-image">
          </div>
        </div>
        <div class="form-group">
          <label>排序</label>
          <input v-model.number="currentPerson.order" type="number" placeholder="排序号">
        </div>
      </div>
      <div class="modal-footer">
        <button class="secondary-button" @click="closePersonModal">取消</button>
        <button class="primary-button" @click="savePerson">保存</button>
      </div>
    </div>
  </div>

  <!-- 企业发展历程弹窗 -->
  <div v-if="showMilestoneModal" class="modal-overlay" @click="closeMilestoneModal">
    <div class="modal-content" @click.stop>
      <div class="modal-header">
        <h3>{{ isEditingMilestone ? '编辑发展历程' : '添加发展历程' }}</h3>
        <button class="modal-close" @click="closeMilestoneModal">&times;</button>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <label data-required="*">年份</label>
          <input v-model="currentMilestone.year" type="text" placeholder="请输入年份">
        </div>
        <div class="form-group">
          <label data-required="*">标题</label>
          <input v-model="currentMilestone.title" type="text" placeholder="请输入标题">
        </div>
        <div class="form-group">
          <label>描述</label>
          <textarea v-model="currentMilestone.description" placeholder="请输入描述" rows="4"></textarea>
        </div>
        <div class="form-group">
          <label>图片</label>
          <div class="image-upload-container">
            <input ref="milestoneFileInput" type="file" accept="image/*" style="display: none" @change="handleMilestoneImageUpload">
            <button class="secondary-button" @click="triggerMilestoneFileInput">选择图片</button>
            <button v-if="currentMilestone.imageUrl" class="delete-button" @click="removeMilestoneImage">移除图片</button>
          </div>
          <div v-if="currentMilestone.imageUrl" class="preview-image-container">
            <img :src="currentMilestone.imageUrl" alt="预览图" class="preview-image">
          </div>
        </div>
        <div class="form-group">
          <label>排序</label>
          <input v-model.number="currentMilestone.order" type="number" placeholder="排序号">
        </div>
      </div>
      <div class="modal-footer">
        <button class="secondary-button" @click="closeMilestoneModal">取消</button>
        <button class="primary-button" @click="saveMilestone">保存</button>
      </div>
    </div>
  </div>

  <!-- 删除确认弹窗 -->
  <div v-if="showDeleteConfirm" class="modal-overlay" @click="closeDeleteConfirm">
    <div class="modal-content" @click.stop>
      <div class="modal-header">
        <h3>确认删除</h3>
        <button class="modal-close" @click="closeDeleteConfirm">&times;</button>
      </div>
      <div class="modal-body">
        <p>确定要删除这条记录吗？此操作不可撤销。</p>
      </div>
      <div class="modal-footer">
        <button class="secondary-button" @click="closeDeleteConfirm">取消</button>
        <button class="danger-button" @click="confirmDeleteAction">删除</button>
      </div>
    </div>
  </div>
</template>

<script>
import CompanyStore from '../../services/CompanyStore';
import AdminLayout from './AdminLayout.vue';

export default {
  name: 'CompanyManagement',
  components: {
    AdminLayout
  },
  data() {
    return {
      activeTab: 'people',
      companyPeople: [],
      companyMilestones: [],
      showPersonModal: false,
      showMilestoneModal: false,
      showDeleteConfirm: false,
      isEditingPerson: false,
      isEditingMilestone: false,
      currentPerson: {
        id: null,
        name: '',
        position: '',
        bio: '',
        imageUrl: '',
        order: 0,
        updatedAt: new Date()
      },
      currentMilestone: {
        id: null,
        year: '',
        title: '',
        description: '',
        imageUrl: '',
        order: 0,
        updatedAt: new Date()
      },
      deleteType: '',
      deleteId: null
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    // 切换标签页
    switchTab(tab) {
      this.activeTab = tab;
    },

    // 加载数据
    loadData() {
      // 加载人物数据
      this.companyPeople = CompanyStore.getAllCompanyPeople();
      
      // 加载历程数据
      this.companyMilestones = CompanyStore.getAllMilestones();
    },

    // 切换人物简介弹窗
    openAddPersonModal() {
      this.isEditingPerson = false;
      this.currentPerson = {
        id: null,
        name: '',
        position: '',
        bio: '',
        imageUrl: '',
        order: 0,
        updatedAt: new Date()
      };
      this.showPersonModal = true;
    },

    // 编辑人物简介
    editPerson(person) {
      this.isEditingPerson = true;
      this.currentPerson = {
        ...person,
        updatedAt: new Date()
      };
      this.showPersonModal = true;
    },

    // 关闭人物简介弹窗
    closePersonModal() {
      this.showPersonModal = false;
      this.isEditingPerson = false;
      this.currentPerson = {
        id: null,
        name: '',
        position: '',
        bio: '',
        imageUrl: '',
        order: 0,
        updatedAt: new Date()
      };
    },

    // 触发文件上传
    triggerPersonFileInput() {
      this.$refs.personFileInput.click();
    },

    // 处理人物图片上传
    handlePersonImageUpload(event) {
      const file = event.target.files[0];
      if (file) {
        // 这里应该上传图片到服务器，获取URL
        // 简化版：使用本地URL
        const reader = new FileReader();
        reader.onload = (e) => {
          this.currentPerson.imageUrl = e.target.result;
        };
        reader.readAsDataURL(file);
      }
    },

    // 移除人物图片
    removePersonImage() {
      this.currentPerson.imageUrl = '';
    },

    // 保存人物简介
    savePerson() {
      if (!this.currentPerson.name || !this.currentPerson.position || !this.currentPerson.imageUrl) {
        alert('请填写必填字段');
        return;
      }

      if (this.isEditingPerson) {
        CompanyStore.updateCompanyPerson(this.currentPerson.id, this.currentPerson);
      } else {
        CompanyStore.addCompanyPerson(this.currentPerson);
      }

      this.loadData();
      this.closePersonModal();
    },

    // 切换发展历程弹窗
    openAddMilestoneModal() {
      this.isEditingMilestone = false;
      this.currentMilestone = {
        id: null,
        year: '',
        title: '',
        description: '',
        imageUrl: '',
        order: 0,
        updatedAt: new Date()
      };
      this.showMilestoneModal = true;
    },

    // 编辑发展历程
    editMilestone(milestone) {
      this.isEditingMilestone = true;
      this.currentMilestone = {
        ...milestone,
        updatedAt: new Date()
      };
      this.showMilestoneModal = true;
    },

    // 关闭发展历程弹窗
    closeMilestoneModal() {
      this.showMilestoneModal = false;
      this.isEditingMilestone = false;
      this.currentMilestone = {
        id: null,
        year: '',
        title: '',
        description: '',
        imageUrl: '',
        order: 0,
        updatedAt: new Date()
      };
    },

    // 触发文件上传
    triggerMilestoneFileInput() {
      this.$refs.milestoneFileInput.click();
    },

    // 处理历程图片上传
    handleMilestoneImageUpload(event) {
      const file = event.target.files[0];
      if (file) {
        // 这里应该上传图片到服务器，获取URL
        // 简化版：使用本地URL
        const reader = new FileReader();
        reader.onload = (e) => {
          this.currentMilestone.imageUrl = e.target.result;
        };
        reader.readAsDataURL(file);
      }
    },

    // 移除历程图片
    removeMilestoneImage() {
      this.currentMilestone.imageUrl = '';
    },

    // 保存发展历程
    saveMilestone() {
      if (!this.currentMilestone.year || !this.currentMilestone.title) {
        alert('请填写必填字段');
        return;
      }

      if (this.isEditingMilestone) {
        CompanyStore.updateMilestone(this.currentMilestone.id, this.currentMilestone);
      } else {
        CompanyStore.addMilestone(this.currentMilestone);
      }

      this.loadData();
      this.closeMilestoneModal();
    },

    // 确认删除
    confirmDelete(type, id) {
      this.deleteType = type;
      this.deleteId = id;
      this.showDeleteConfirm = true;
    },

    // 关闭删除确认弹窗
    closeDeleteConfirm() {
      this.showDeleteConfirm = false;
      this.deleteType = '';
      this.deleteId = null;
    },

    // 执行删除操作
    confirmDeleteAction() {
      if (this.deleteType === 'people') {
        CompanyStore.deleteCompanyPerson(this.deleteId);
      } else if (this.deleteType === 'milestones') {
        CompanyStore.deleteMilestone(this.deleteId);
      }

      this.loadData();
      this.closeDeleteConfirm();
    },

    // 格式化日期
    formatDate(dateString) {
      if (!dateString) return '';
      const date = new Date(dateString);
      return date.toLocaleDateString('zh-CN');
    }
  }
};
</script>

<style scoped>
/* 统一管理页面容器样式 */
.admin-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
}

/* 统一标题头部样式 */
.page-header {
  text-align: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #e0e0e0;
}

.page-header h1 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.page-description {
  font-size: 14px;
  color: #666;
  margin: 0;
}

/* 标签切换容器 */
.tab-container {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
  border-bottom: 1px solid #e0e0e0;
}

.tab-button {
  padding: 12px 24px;
  border: none;
  background: none;
  font-size: 14px;
  font-weight: 500;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
  border-bottom: 2px solid transparent;
  margin: 0 8px;
}

.tab-button:hover {
  color: #4CAF50;
}

.tab-button.active {
  color: #4CAF50;
  border-bottom-color: #4CAF50;
}

/* 搜索和操作按钮区域 */
.search-actions {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}

/* 内容区域 */
.content-wrapper {
  background-color: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 人物网格 */
.people-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.person-item {
  display: flex;
  flex-direction: column;
  padding: 16px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  transition: all 0.3s;
  background-color: #fff;
}

.person-item:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.person-image-container {
  width: 100%;
  height: 200px;
  overflow: hidden;
  border-radius: 4px;
  margin-bottom: 16px;
}

.person-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.person-item:hover .person-image {
  transform: scale(1.05);
}

.person-info {
  flex: 1;
}

.person-info h3 {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.person-position {
  font-size: 14px;
  color: #666;
  margin: 0 0 12px 0;
}

.person-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  font-size: 12px;
  color: #999;
}

.person-actions {
  display: flex;
  gap: 8px;
}

/* 发展历程列表 */
.milestones-list {
  border-left: 2px solid #e0e0e0;
  padding-left: 20px;
  margin-left: 20px;
}

.milestone-item {
  display: flex;
  gap: 20px;
  padding-bottom: 24px;
  position: relative;
  margin-bottom: 24px;
}

.milestone-item::before {
  content: '';
  position: absolute;
  left: -29px;
  top: 0;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: #4CAF50;
  border: 3px solid #e8f5e9;
}

.milestone-year {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  transform: rotate(180deg);
  min-width: 60px;
  font-size: 24px;
  font-weight: 700;
  color: #4CAF50;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.milestone-content {
  flex: 1;
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
}

.milestone-content h3 {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 12px 0;
}

.milestone-image {
  margin-bottom: 16px;
  border-radius: 4px;
  overflow: hidden;
  max-width: 200px;
}

.small-image {
  width: 100%;
  height: auto;
}

.milestone-description {
  font-size: 14px;
  line-height: 1.6;
  color: #666;
  margin: 0 0 16px 0;
}

.milestone-meta {
  margin-bottom: 12px;
  font-size: 12px;
  color: #999;
}

.milestone-actions {
  display: flex;
  gap: 8px;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

/* 按钮样式 */
.primary-button {
  background-color: #4CAF50;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background-color 0.3s;
}

.primary-button:hover {
  background-color: #45a049;
}

.secondary-button {
  background-color: #f5f5f5;
  color: #333;
  border: 1px solid #e0e0e0;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.secondary-button:hover {
  background-color: #e0e0e0;
}

.danger-button {
  background-color: #f44336;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.danger-button:hover {
  background-color: #d32f2f;
}

.edit-button {
  background-color: #f5f5f5;
  color: #333;
  border: 1px solid #e0e0e0;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.3s;
}

.edit-button:hover {
  background-color: #e0e0e0;
}

.delete-button {
  background-color: #f44336;
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: background-color 0.3s;
}

.delete-button:hover {
  background-color: #d32f2f;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-container {
    padding: 15px;
  }

  .page-header h1 {
    font-size: 20px;
  }

  .tab-container {
    flex-wrap: wrap;
  }

  .tab-button {
    margin: 4px;
  }

  .content-wrapper {
    padding: 16px;
  }

  .people-grid {
    grid-template-columns: 1fr;
  }

  .milestones-list {
    border-left: none;
    padding-left: 0;
    margin-left: 0;
  }

  .milestone-item {
    flex-direction: column;
    gap: 12px;
  }

  .milestone-item::before {
    left: 0;
    top: 0;
  }

  .milestone-year {
    writing-mode: horizontal-tb;
    transform: none;
    min-width: auto;
    font-size: 20px;
  }

  .milestone-image {
    max-width: 100%;
  }

  .search-actions {
    justify-content: center;
  }
}
/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background-color: #fff;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #e0e0e0;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.3s;
}

.modal-close:hover {
  background-color: #f5f5f5;
  color: #666;
}

.modal-body {
  padding: 24px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 24px;
  border-top: 1px solid #e0e0e0;
  background-color: #fafafa;
  border-bottom-left-radius: 8px;
  border-bottom-right-radius: 8px;
}

/* 表单样式 */
.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
  box-sizing: border-box;
}

.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #4CAF50;
}

.form-group textarea {
  resize: vertical;
  min-height: 80px;
}

/* 图片上传样式 */
.image-upload-container {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.preview-image-container {
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 8px;
  max-width: 200px;
}

.preview-image {
  width: 100%;
  height: auto;
  border-radius: 4px;
}

/* 必填项星号样式 */
.form-group label::after {
  content: attr(data-required);
  color: #f44336;
  margin-left: 4px;
}
</style>